<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Equipment Config</title>
	<link rel="stylesheet" href="../js/easyui/themes/bootstrap/easyui.css">
	<link rel="stylesheet" href="../js/easyui/themes/icon.css">
	<link rel="stylesheet" href="../css/index-ui.css">
	<script type="text/javascript" src="../js/easyui/jquery.min.js"></script>
	<script type="text/javascript" src="../js/easyui/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../js/ams.util.js"></script>
	<script type="text/javascript" src="../js/webapi.js"></script>
</head>
<body onload="page_init()">
	<div id="equip_settings" style="padding:5px;padding-left:1px;padding-right:0px">
		<div disabled=true style="float:left;padding-left:0px">
			<a id="grpAdd" class="easyui-linkbutton" style="width:80px" onclick="on_add_group()">Add</a>
		</div>
		<div style="float:left;padding-left:10px">
			<a id="grpDel" class="easyui-linkbutton" style="width:80px" onclick="on_delete_group()">Delete</a>
		</div>
		<div style="float:left;padding-left:10px">
			<a id="grpRename" class="easyui-linkbutton" style="width:80px" onclick="on_rename_group()">Rename</a>
		</div>
		<div style="float:right;padding-right:0px">
			<a id="eqInit" class="easyui-linkbutton" style="width:80px" onclick="on_init_equip()">Initialize</a>
		</div>
		<div style="float:right;padding-right:10px">
			<a id="eqDel" class="easyui-linkbutton" style="width:80px" onclick="on_delete_equip()">Delete</a>
		</div>
		<div style="float:right;padding-right:10px">
			<a id="eqModify" class="easyui-linkbutton" style="width:80px" onclick="on_modify_equip()">Modify</a>
		</div>
		<div style="float:right;padding-right:10px">
			<a id="eqAdd" class="easyui-linkbutton" style="width:80px" onclick="on_add_equip()">Add</a>
		</div>
	</div>
	<br/><br/>
	<div id="eq_man_layout_main" class="easyui-layout cls_rcp_layout_main">
		<div data-options="region:'west',split:true" title="" style="width:265px;">
			<table id="eq_group_list_grid" class="easyui-datagrid" title="" style="width:100%;height:90%" pagination="false">
				<thead>
					<tr>
						<th data-options="field:'group_name',width:250,align:'left'">Group Name</th>
						<th data-options="field:'group_id',width:50,align:'left',hidden:true">uid</th>
					</tr>
				</thead>
			</table>
			 <div id="grp_win" class="easyui-window" title="" data-options="modal:true, closed:true" style="width:280px;height:190px;">
	    	<form style="padding:20px 20px 10px 40px;">
	    		<p>Name: <input class="easyui-textbox" id="text_grp_name" name="text_grp_name" type="text"></p>
	    		<div style="padding:30px;text-align:center;">
	    			<a href="#" class="easyui-linkbutton" icon="icon-ok" onclick="do_grp_man_ok()">Ok</a>
	    			<a href="#" class="easyui-linkbutton" icon="icon-cancel" onclick="do_grp_man_cancel()">Cancel</a>
	    		</div>
	    	</form>
	    </div>
		</div>>
		<div data-options="region:'center',split:true" title="" style="width:400px;">
			<table id="eq_management_list_grid" class="easyui-datagrid" title="" style="width:100%;height:90%" pagination="false">
				<thead>
					<tr>
						<!-- <th data-options="field:'no',width:40">No</th> -->
						<th data-options="field:'id',width:140">Id</th>
						<th data-options="field:'ip',width:140,align:'left'">Ip</th>
						<th data-options="field:'port',width:60,align:'left'">Port</th>
						<th data-options="field:'type',width:300,align:'left'">Type</th>
						<th data-options="field:'uid',width:50,align:'left',hidden:true">uid</th>
					</tr>
				</thead>
			</table>
			<div id="equip_win" class="easyui-window" title="" data-options="modal:true, closed:true" style="width:280px;height:300px;">
	    	<form style="padding:20px 20px 10px 40px;">
	    		<h2>Equipment information:</h2>
					<div style="margin:20px 20px 20px 20px;"></div>
					<div style="margin-bottom:10px">
						<p><input class="easyui-textbox" id="text_equip_id" data-options="prompt:'id...'" style="width:180px;height:24px;"></p>
					</div>
					<div style="margin-bottom:10px">
						<p><input class="easyui-textbox" id="text_equip_ip" data-options="prompt:'ip address...'" style="width:180px;height:24px;"></p>
					</div>
					<div style="margin-bottom:10px">
						<p><input class="easyui-textbox" id="text_equip_port" data-options="prompt:'port...'" style="width:180px;height:24px;"></p>
					</div>
					<div style="margin-bottom:10px">
						<p><input class="easyui-textbox" id="text_equip_type" data-options="prompt:'type...'" style="width:180px;height:24px;"></p>
					</div>
	    		<div style="padding:10px;text-align:center;">
	    			<a href="#" class="easyui-linkbutton" icon="icon-ok" onclick="do_equip_man_ok()">Ok</a>
	    			<a href="#" class="easyui-linkbutton" icon="icon-cancel" onclick="do_equip_man_cancel()">Cancel</a>
	    		</div>
	    	</form>
	    </div>
		</div>>
	</div>
	<input type="hidden" id="GroupName">
	<input type="hidden" id="GroupId">
	<!--
	-->
</body>

<script language="javascript"> 

function page_init()
{
	// disable button
	$('#grpAdd').linkbutton('disable')
	$('#grpDel').linkbutton('disable')
	$('#grpRename').linkbutton('disable')
	$('#eqAdd').linkbutton('disable')
	$('#eqModify').linkbutton('disable')
	$('#eqDel').linkbutton('disable')
	$('#eqInit').linkbutton('disable')
	
	// left list
	$('#eq_group_list_grid').datagrid({
		onSelect: function(rowIndex, rowData) {
			console.log(rowIndex)
			console.log(rowData)
			$("#GroupName").val(rowData.group_name)
			$("#GroupId").val(rowData.group_id)
		//	alert('click file: ' + row.f)
			on_refresh_group_content()
		}
	})

	on_refresh_grp_info()
}

function on_refresh_grp_info()
{
	// var query_url = ''

	$('#eq_group_list_grid').datagrid({
		//	rownumbers:'true',
		singleSelect:'true',
		lines:true,
		clickToEdit: false,
		dblclickToEdit: false,
		loadMsg: 'Loading Data ......',
		method:'get'
	});

	$('#eq_group_list_grid').datagrid('getPager').pagination({
		showPageList: false,
        showRefresh: false,
        displayMsg: ''
	})

	if (ams_is_sim_mode()) {
		// query_url = 'group_files_01.json'
		// $('#eq_group_list_grid').datagrid('loadData',query_url.rows);
		$.getJSON('group_files_01.json', function(json_data){
			$('#eq_group_list_grid').datagrid('loadData',json_data.rows);
		})
	} else {
		$.ajax({
			type: 'POST',
			contentType: 'application/json',
			url: getWebServiceUrl("getEqGroupList"),	
			dataType: 'json',
			success : function(datas) {
				// console.log(getData())
				console.log(JSON.parse(datas.d))
				$('#eq_group_list_grid').datagrid('loadData',JSON.parse(datas.d));
				
			},
			error: function (error) {
				//alert(errorThrown.message);
				console.log(error) 
			}
		});
	}

}

function on_add_group()
{
	$('#grp_win').window({title:'Add'}).window('open');
}

function on_delete_group()
{
	var row = $("#eq_group_list_grid").datagrid('getSelected')
	if (!row) {
		$.messager.alert("Tips", "Select a group please.", "info");
		return
	}
	else
	{
		console.log(row);
		$.messager.confirm('Confirm', 'Are you sure you want to delete it?', function(r){
			if (r){
				if (ams_is_sim_mode()) {
					$.messager.alert("Tips", "Delete successful.", "info");
				}else{
					$.ajax({
						type: 'POST',
						contentType: 'application/json',
						url: getWebServiceUrl("cmdGroupInfo"),	
						data:JSON.stringify({"cmd":2,"grpId":row.group_id,"grpName":row.group_name}),
						dataType: 'json',
						success : function(datas) {
							// console.log(getData())
							console.log(datas.d)
							if(datas.d == "0"){
								$.messager.alert("Tips", "Delete successful.", "info");
								on_refresh_grp_info()
								$('#eq_management_list_grid').datagrid('loadData',{total:0,rows:[]});
							}else if(datas.d == "-1"){
								$.messager.alert("Tips", "Delete failed.", "info");
							}
						},
						error: function (error) {
							//alert(errorThrown.message);
							console.log(error) 
						}
					});
				}
			}
		});
	}
}

function on_rename_group()
{
	var row = $("#eq_group_list_grid").datagrid('getSelected')
	if (!row) {
		$.messager.alert("Tips", "Select a group please.", "info");
		return
	}
	
	$('#grp_win').window({title:'Rename'}).window('open');
	$('#text_grp_name').textbox('setValue', row.f);
}

function on_refresh_group_content()
{
	var GroupName = $("#GroupName").val();
	console.log(GroupName)
	$('#eq_management_list_grid').datalist('loadData',[])

	// var query_url = ''

	$('#eq_management_list_grid').datagrid({
		rownumbers:'true',
		singleSelect:'true',
		lines:true,
		clickToEdit: false,
		dblclickToEdit: false,
		loadMsg: 'Loading Data ......',
		method:'get'
	});

	if (ams_is_sim_mode()) {
		// query_url = 'equipment_01.json'
		$.getJSON('equipment_01.json', function(json_data){
			$('#eq_management_list_grid').datagrid('loadData',json_data.rows);
		})
	} else {
		$.ajax({
			type: 'POST',
			contentType: 'application/json',
			url: getWebServiceUrl("getEqGroupDetailList"),	
			data:JSON.stringify({"grpName":GroupName}),
			dataType: 'json',
			success : function(datas) {
				// console.log(getData())
				console.log(datas.d)
				$('#eq_management_list_grid').datagrid('loadData',JSON.parse(datas.d));
				// if(JSON.parse(datas.d).length==10){
	   //              count+=10;
	   //              $('#pp').pagination({
				// 		total:count
				// 	});
				// }
				// console.log(count)
				
			},
			error: function (error) {
				//alert(errorThrown.message);
				console.log(error) 
			}
		});
	}
}

function do_grp_man_ok()
{
	var opts = $('#grp_win').panel('options');
	var title = opts.title;//»ñÈ¡titleÊôÐÔ
	$('#grp_win').window('close')
	var grpName = $("#text_grp_name").textbox('getValue');
	if (title == "Add")
	{
		if (ams_is_sim_mode()) {
			$.messager.alert("Tips", "Add success.", "info");
		}else{
			$.ajax({
				type: 'POST',
				contentType: 'application/json',
				url: getWebServiceUrl("cmdGroupInfo"),	
				data:JSON.stringify({"cmd":1,"grpId":"","grpName":grpName}),
				dataType: 'json',
				success : function(datas) {
					// console.log(getData())
					console.log(datas.d)
					if(datas.d == "0"){
						$.messager.alert("Tips", "Add success.", "info");
						on_refresh_grp_info()
					}else if(datas.d == "1"){
						$.messager.alert("Tips", "Add failure.", "info");
					}
				},
				error: function (error) {
					//alert(errorThrown.message);
					console.log(error) 
				}
			});
		}
	}
	else
	{
		var selVal = $('#eq_group_list_grid').datalist('getSelected');
		if (ams_is_sim_mode()) {
			$.messager.alert("Tips", "Modified success.", "info");
		}else{
			$.ajax({
				type: 'POST',
				contentType: 'application/json',
				url: getWebServiceUrl("cmdGroupInfo"),	
				data:JSON.stringify({"cmd":3,"grpId":selVal.group_id,"grpName":grpName}),
				dataType: 'json',
				success : function(datas) {
					// console.log(getData())
					console.log(datas.d)
					if(datas.d == "0"){
						$.messager.alert("Tips", "Modified success.", "info");
						on_refresh_grp_info()
					}else if(datas.d == "1"){
						$.messager.alert("Tips", "Modified failed.", "info");
					}
				},
				error: function (error) {
					//alert(errorThrown.message);
					console.log(error) 
				}
			});
		}
	}
}

function do_grp_man_cancel()
{
	$('#grp_win').window('close')
}

function on_add_equip()
{
	var row = $("#eq_group_list_grid").datagrid('getSelected')
	if (!row) {
		$.messager.alert("Tips", "Select a group please.", "info");
		return
	}
	
	$('#equip_win').window({title:'Add'}).window('open');
}

function on_modify_equip()
{
	var row = $("#eq_management_list_grid").datagrid('getSelected')
	if (!row) {
		$.messager.alert("Tips", "Select a equipment please.", "info");
		return
	}

	$('#equip_win').window({title:'Modify'}).window('open');
	$('#text_equip_id').textbox('setValue', row.id);
	$('#text_equip_ip').textbox('setValue', row.ip);
	$('#text_equip_port').textbox('setValue', row.port);
	$('#text_equip_type').textbox('setValue', row.type);
}

function on_delete_equip()
{
	var row = $("#eq_management_list_grid").datagrid('getSelected')
	if (!row) {
		$.messager.alert("Tips", "Select a equipment please.", "info");
		return
	}
	else
	{
		// alert('delete equipment: ' + row.id)
		console.log(row)

		$.messager.confirm('Confirm', 'Are you sure you want to delete it?', function(r){
			if (r){
				if (ams_is_sim_mode()) {
					$.messager.alert("Tips", "Delete successful.", "info");
				}else{
					$.ajax({
						type: 'POST',
						contentType: 'application/json',
						url: getWebServiceUrl("cmdEqInfo"),	
						data:JSON.stringify({"cmd":2,"uid":row.uid,"grpName":$("#GroupName").val(),"eqId":row.id,"ip":row.ip,"port":row.port,"type":row.type}),
						dataType: 'json',
						success : function(datas) {
							// console.log(getData())
							console.log(datas.d)
							if(datas.d == "0"){
								$.messager.alert("Tips", "Delete successful.", "info");
								on_refresh_group_content()
							}else if(datas.d == "1"){
								$.messager.alert("Tips", "Delete failed.", "info");
							}
						},
						error: function (error) {
							//alert(errorThrown.message);
							console.log(error) 
						}
					});
				}
			}
		});
	}
}

function on_init_equip()
{
	var row = $("#eq_management_list_grid").datagrid('getSelected')
	if (!row) {
		$.messager.alert("Tips", "Select a equipment please.", "info");
		return
	}

		if (ams_is_sim_mode()) {
			$.messager.alert("Tips", "Initialize success.", "info");
		}else{
			$.messager.confirm('AMS System',
			'Are you sure to initialize "' + row.id + '" ?',
			function(r) {
				if (r)
					$.ajax({
					type: 'POST',
					contentType: 'application/json',
					url: getWebServiceUrl("initEquip"),	
					data:JSON.stringify({"eqName":row.id}),
					dataType: 'json',
					success : function(datas) {
						// console.log(getData())
						console.log(datas.d)
						if(datas.d == "0"){
							$.messager.alert("Tips", "Initialize success.", "info");
							on_refresh_group_content()
						}else if(datas.d == "-1"){
							$.messager.alert("Tips", "Initialize failure.", "info");
						}
					},
					error: function (error) {
						//alert(errorThrown.message);
						console.log(error) 
					}
				});
			})
		}
}

function do_equip_man_ok()
{
	var row = $("#eq_management_list_grid").datagrid('getSelected')
	console.log(row)
	var opts = $('#equip_win').panel('options');
	var title = opts.title;//»ñÈ¡titleÊôÐÔ
	$('#equip_win').window('close')
	var id = $("#text_equip_id").textbox('getValue');
	var ip = $("#text_equip_ip").textbox('getValue');
	var port = $("#text_equip_port").textbox('getValue');
	var type = $("#text_equip_type").textbox('getValue');
	if (title == "Add")
	{
		if (ams_is_sim_mode()) {
			$.messager.alert("Tips", "Add success.", "info");
		}else{
			$.ajax({
				type: 'POST',
				contentType: 'application/json',
				url: getWebServiceUrl("cmdEqInfo"),	
				data:JSON.stringify({"cmd":1,"uid":"","grpName":$("#GroupName").val(),"eqId":id,"ip":ip,"port":port,"type":type}),
				dataType: 'json',
				success : function(datas) {
					// console.log(getData())
					console.log(datas.d)
					if(datas.d == "0"){
						$.messager.alert("Tips", "Add success.", "info");
						on_refresh_group_content()
					}else if(datas.d == "-1"){
						$.messager.alert("Tips", "Add failure.", "info");
					}
				},
				error: function (error) {
					//alert(errorThrown.message);
					console.log(error) 
				}
			});
		}

	}
	else if (title == "Modify")
	{
		if (ams_is_sim_mode()) {
			$.messager.alert("Tips", "Modified success.", "info");
		}else{
			$.ajax({
				type: 'POST',
				contentType: 'application/json',
				url: getWebServiceUrl("cmdEqInfo"),	
				data:JSON.stringify({"cmd":3,"uid":row.uid,"grpName":$("#GroupName").val(),"eqId":id,"ip":ip,"port":port,"type":type}),
				dataType: 'json',
				success : function(datas) {
					// console.log(getData())
					console.log(datas.d)
					if(datas.d == "0"){
						$.messager.alert("Tips", "Modified success.", "info");
						on_refresh_group_content()
					}else if(datas.d == "-1"){
						$.messager.alert("Tips", "Modified failed.", "info");
					}
				},
				error: function (error) {
					//alert(errorThrown.message);
					console.log(error) 
				}
			});
		}
	}
	else
	{
		var selVal = $('#eq_group_list_grid').datalist('getSelected');
		alert(selVal['uid']);
	}
}

function do_equip_man_cancel()
{
	$('#equip_win').window('close')
}

</script>